#include <iostream>
#include "ContactServer.hpp"
#include "../../cpp-httplib/httplib.h"

int main()
{
    std::cout << "----服务启动----" << std::endl;
    ContactServer conserver;
    httplib::Server svr;
    // 新添联系人
    svr.Post("/contacts/add", [conserver](const httplib::Request &req, httplib::Response &rep)
             {
        request::AddContactRequest requ;
        response::AddContactResponse resp;
        try {
            //序列化请求
            if(!requ.ParseFromString(req.body)){
                throw ContactException("parse AddContactRequest error!");
            }
            conserver.add(requ,&resp);
            std::string rep_str;

            //反序列化响应
            //构建响应
            if(!resp.SerializeToString(&rep_str)){
                throw ContactException("Serialize AddContactResponse error");
            }
            rep.body=rep_str;
            rep.set_header("Content-Type", "application/protobuf");
            rep.status=200;
        }
        catch(const ContactException& e){
            std::cerr << "---> /contacts/add 发现异常!!!" << std::endl<< "---> 异常信息：" << e.what() << std::endl;
            rep.status=500;
            response::BaseResponse* base=resp.mutable_base();
            base->set_success(false);
            base->set_err_desc(e.what());
            std::string response_str;
            if(resp.SerializeToString(&response_str)){
                rep.body=response_str;
                rep.set_header("Content-Type", "application/protobuf");
            }
        } });

    svr.Post("/contacts/del", [conserver](const httplib::Request &req, httplib::Response &rep)
             {
        request::DelContactRequest requ;
        response::DelContactRequest resp;
        try
        {
            if (!requ.ParseFromString(req.body))
            {
                throw ContactException("parse AddContactRequest error!");
            }

            // 删除联系人
            conserver.del(requ, &resp);
            std::string rep_str;

            // 反序列化响应
            // 构建响应
            if (!resp.SerializeToString(&rep_str))
            {
                throw ContactException("Serialize DelContactResponse error");
            }
            rep.body = rep_str;
            rep.status = 200;
            rep.set_header("Content-Type", "application/protobuf");
        }
        catch (const ContactException &e)
        {
            std::cerr << "---> /contacts/del 发现异常!!!" << std::endl
                      << "---> 异常信息：" << e.what() << std::endl;
            rep.status = 500;
            response::BaseResponse *base = resp.mutable_base();
            base->set_success(false);
            base->set_err_desc(e.what());
            std::string response_str;
            if (resp.SerializeToString(&response_str))
            {
                rep.body = response_str;
                rep.set_header("Content-Type", "application/protobuf");
            }
        } });

    svr.Post("/contacts/find-one", [conserver](const httplib::Request &req, httplib::Response &rep)
             {
        request::FindOneContactRequest requ;
        response::FindOneContactResponse resp;
        try
        {
            if (!requ.ParseFromString(req.body))
            {
                throw ContactException("Parse FindOneContactRequest error!");
            }

            // 查找一个联系人
            conserver.findone(requ, &resp);
            std::string rep_str;

            // 反序列化响应
            // 构建响应
            if (!resp.SerializeToString(&rep_str))
            {
                throw ContactException("Serialize FindOneContactResponseerror");
            }
            rep.body = rep_str;
            rep.status = 200;
            rep.set_header("Content-Type", "application/protobuf");
        }
        catch (const ContactException &e)
        {
            std::cerr << "---> /contacts/find-one 发现异常!!!" << std::endl
                      << "---> 异常信息：" << e.what() << std::endl;
            rep.status = 500;
            response::BaseResponse *base =resp.mutable_base();
            base->set_success(false);
            base->set_err_desc(e.what());
            std::string response_str;
            if (resp.SerializeToString(&response_str))
            {
                rep.body = response_str;
                rep.set_header("Content-Type", "application/protobuf");
            }
        } });

    svr.Get("/contacts/find-all", [conserver](const httplib::Request &req, httplib::Response &rep)
            {
        response::FindAllContactsResponse resp;
        ContactServer conserver;
        try{
            //查找所有联系人
            conserver.findall(&resp);
            std::string resp_str;

            //反序列化响应
            //构建响应
            if (!resp.SerializeToString(&resp_str)) {
                throw ContactException("Serialize FindAllContactsResponseerror");
            }
            rep.body=resp_str;
            rep.status=200;
            rep.set_header("Content-Type", "application/protobuf");
        }
        catch(const ContactException& e){
            std::cerr << "---> /contacts/find-all 发现异常!!!" <<std::endl<< "---> 异常信息：" << e.what() << std::endl;
            rep.status = 500;
            response::BaseResponse* base =resp.mutable_base();
            base->set_success(false);
            base->set_err_desc(e.what());
            std::string response_str;
            if (resp.SerializeToString(&response_str)) {
                rep.body = response_str;
                rep.set_header("Content-Type", "application/protobuf");
            }
        } });

        //监听端口
        svr.listen(CONTACTS_IP,CONTACTS_PORT);
}
